package com.df.ez_game.server.logic;

import com.df.ez_game.business.annotation.EzController;
import com.df.ez_game.server.NetProtocType;
import com.df.ez_game.server.NodeType;
import com.df.ez_game.server.ServerManager;
import com.df.ez_game.server.ServerNode;
import jakarta.annotation.PreDestroy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.net.InetSocketAddress;
import java.util.Map;

@SpringBootApplication
public class LogicMain {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(LogicMain.class);
        Map<String, Object> beansWithAnnotation = context.getBeansWithAnnotation(EzController.class);

        //下面可以配置成一个bean
        LogicServerNode logicServerNode = new LogicServerNode();
        LogicSettings settings = logicServerNode.getSettings();

        settings.setNodeType(NodeType.SingletonLogic)
                .setPort(13000)
                .setNetProtoc(NetProtocType.WEBSOCKET.getValue())
                .setWsPath("/ws")
                .getControllerInstance().addAll(beansWithAnnotation.values());
        settings.getStartFailCb().add(channel -> {
            System.out.println("启动失败！");
        });
        settings.getStartSuccessCb().add(channel -> {
            InetSocketAddress addr = (InetSocketAddress) channel.localAddress();
            System.out.println("启动成功！"+addr.getHostString()+"："+addr.getPort()+" path:"+settings.getWsPath());
        });
        logicServerNode.start();
    }

    @PreDestroy
    void close(){
        ServerNode serverNode = ServerManager.getServerNode("default");
        if(serverNode!=null){
            serverNode.shutDown();
        }
    }
}
